/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boxin.ims.modules.wechat.dao.WechatBusinessDao;
import com.boxin.ims.modules.wechat.entity.BusinessConfig;
import com.boxin.ims.modules.wechat.entity.TextMessage;
import com.boxin.ims.modules.wechat.entity.UserBusiness;
import com.boxin.ims.modules.wechat.entity.WeChat;
import com.boxin.ims.modules.wechat.entity.WechatBusiness;
import com.boxin.ims.modules.wechat.entity.WechatConfig;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.thinkgem.jeesite.modules.sys.entity.User;

/**
 * 微信业务Service
 * @author Jakemanse
 * @version 2013-10-23
 */
@Component
@Transactional(readOnly = true)
public class WechatBusinessService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(WechatBusinessService.class);
	
	@Autowired
	private WechatBusinessDao wechatBusinessDao;
	
	@Autowired
	private WechatConfigService wechatConfigService;
	
	@Autowired
	private WeChatService weChatService;
	
	@Autowired
	private UserBusinessService userBusinessService;
	

	public WechatBusiness get(Long id) {
		return wechatBusinessDao.findOne(id);
	}
	
	public WechatBusiness getByUserId(Long userId) {
		return wechatBusinessDao.getByUserId(userId);
	}
	
	public Page<WechatBusiness> find(Page<WechatBusiness> page, WechatBusiness wechatBusiness) {
		DetachedCriteria dc = wechatBusinessDao.createDetachedCriteria();
		if (wechatBusiness.getUser()!=null && wechatBusiness.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", wechatBusiness.getUser().getId()));
		}
		dc.add(Restrictions.eq("delFlag", WechatBusiness.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return wechatBusinessDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(WechatBusiness wechatBusiness) {
		wechatBusinessDao.save(wechatBusiness);
		//此处需要保存 关键信息
		List<BusinessConfig> businessConfigList =  	wechatBusiness.getBusinessConfigList();
		
		
		
		
		User user = wechatBusiness.getUser();
		WeChat	wx = weChatService.getWeChatByUserId(user.getId());
		
		userBusinessService.invalidAllByUserId(user.getId());	//先将该用户的都服务都置成无效
		
		for(BusinessConfig config : businessConfigList){
//			//添加WechatConfig
//			if(wx != null){
//				WechatConfig wechatConfig = wechatConfigService.getByBusinessConfigId(wx.getId(), config.getId());
//				if(wechatConfig==null){
//					wechatConfig = new WechatConfig();
//					wechatConfig.setWeChat(wx);
//					wechatConfig.setMsgType(TextMessage.MSG_TYPE_BUSINISS);
//					wechatConfig.setUpContent(config.getKeywords());
//					
//					wechatConfigService.save(wechatConfig);
//				}
//			}
			
			//添加UserBusiness
			
			UserBusiness userBusiness = userBusinessService.getByUserIdAndConfigId(user.getId(), config.getId());
			if(userBusiness!=null){	//已存在，则更新
				userBusiness.setDelFlag(UserBusiness.DEL_FLAG_NORMAL);
				userBusinessService.save(userBusiness);
			}else{
				userBusiness = new UserBusiness();
				userBusiness.setUser(user);
				userBusiness.setBusinessConfig(config);
				userBusiness.setName(config.getKeywords());
				userBusinessService.save(userBusiness);
			}
			
		}
		
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		wechatBusinessDao.deleteById(id);
	}
	
}
